package starter.kit.rx.app.feature.film;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import starter.kit.feature.StarterFragment;
import starter.kit.rx.app.R;

/**
 * ============================================================
 * 版权： xx有限公司 版权所有（c）2016
 * <p>
 * 作者：Loofer
 * 版本：1.0
 * 创建日期 ：2016/10/23 11:14.
 * 描述：电影榜单顶部第一个tab
 * <p>
 * 注:如果您修改了本类请填写以下内容作为记录，如非本人操作劳烦通知，谢谢！！！
 * Modified Date Modify Content:
 * <p>
 * ==========================================================
 */
public class FilmFragment extends StarterFragment implements ViewPager.OnPageChangeListener {


    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager mViewPager;
    // TabLayout中的tab标题
    private String[] mTitles;
    // 填充到ViewPager中的Fragment
    private List<Fragment> mFragments;
    //    private FilmLiveFragment filmLiveFragment;
//    private FilmTop250Fragment filmTop250Fragment;
    //private FilmGodFragment filmGodFragment;
    private FilmPagerAdapter mFilmPagerAdapter;

    public static FilmFragment newInstance() {
        Bundle args = new Bundle();
        FilmFragment fragment = new FilmFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_film;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupViewPager();
        setupTabLayout();
    }


    /**
     * 初始化viewpager
     */
    private void setupViewPager() {
        //初始化填充到ViewPager中的Fragment集合

//        filmLiveFragment=FilmLiveFragment.newInstance();
//        filmTop250Fragment=FilmTop250Fragment.newInstance() ;
        // filmGodFragment=FilmGodFragment.newInstance();

        mFragments = new ArrayList<>();
        mFragments.add(FilmLiveFragment.create());
        mFragments.add(FilmTop250Fragment.create());
        mTitles = getResources().getStringArray(R.array.tab_film);

        mFilmPagerAdapter = new FilmPagerAdapter(getChildFragmentManager(), mFragments, mTitles);
        mViewPager.setAdapter(mFilmPagerAdapter);
        // 设置ViewPager最大缓存的页面个数
        mViewPager.setOffscreenPageLimit(3);
        // 给ViewPager添加页面动态监听器（为了让Toolbar中的Title可以变化相应的Tab的标题）
        mViewPager.addOnPageChangeListener(this);

//        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, RxApp.appResources().getDisplayMetrics());
//        mViewPager.setPageMargin(pageMargin);
    }

    /**
     * 初始化tablayout
     */
    private void setupTabLayout() {
        tablayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tablayout.setTabMode(TabLayout.MODE_FIXED);
//        tablayout.setSelectedTabIndicatorColor(ThemeUtils.getThemeColor());
//        tablayout.setTabTextColors(getResources().getColor(R.color.text_gray_6),ThemeUtils.getThemeColor());
        // 将TabLayout和ViewPager进行关联，让两者联动起来
        tablayout.setupWithViewPager(mViewPager);
        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
//        tablayout.setTabsFromPagerAdapter(mFilmPagerAdapter);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
